/**
 * @author Justin Akin
 * @version OpenLogger 0.1.0
 */
package com.actionpals.kit.openlogger
{
	import flash.events.StatusEvent;	
	import flash.net.LocalConnection;	
	
	/**
	 * 
	 */
	public class OpenLogger
	{
		
	//--------------------------------------------------------------------------------
	// Public Static Member Constant
	//--------------------------------------------------------------------------------
	
		public static const CONNECTION_NAME:String = "_OpenLogger_Connection";
		
	//--------------------------------------------------------------------------------
	// Protected Static Member Variables
	//--------------------------------------------------------------------------------
	
		protected static var _singleton:OpenLogger;
		
	//--------------------------------------------------------------------------------
	// Protected Member Variables
	//--------------------------------------------------------------------------------
	
		protected var _localConnection:LocalConnection;

	//--------------------------------------------------------------------------------
	// Constructor
	// NOTE: AS3 does not allow for private or protected constructors
	//--------------------------------------------------------------------------------
	
		/**
		 * Singleton constructor.  Can only be called by the static getInstance method.
		 * 
		 * @param caller	The function to call the Dragger constructor function
		 */
		public function OpenLogger( caller:Function = null )
		{
			
			if( caller != OpenLogger.getInstance )
				throw new Error ( "_singleton is a _singleton class, use getInstance() instead" );
				
	        if ( OpenLogger._singleton != null )
				throw new Error( "Only one _singleton instance should be instantiated" );
				
			_localConnection = new LocalConnection();
			_localConnection.addEventListener( StatusEvent.STATUS, onStatus );	
		}
		
	//--------------------------------------------------------------------------------
	// Public Static Functions
	//-------------------------------------------------------------------------------- 
		
		/**
		 * 
		 */
		public static function log( sender:String, input:* ):void
		{
			getInstance().send( "log", sender, input );
		}
		
		/**
		 * 
		 */
		public static function info( sender:String, input:* ):void
		{
			getInstance().send( "info", sender, input );
		}
		
		/**
		 * 
		 */
		public static function warn( sender:String, input:* ):void
		{
			getInstance().send( "warn", sender, input );
		}
		
		/**
		 * 
		 */
		public static function debug( sender:String, input:* ):void
		{
			getInstance().send( "debug", sender, input );
		}
		
		/**
		 * 
		 */
		public static function error( sender:String, input:* ):void
		{
			getInstance().send( "error", sender, input );
		}
		
		/**
		 * 
		 */
		public static function fatal( sender:String, input:* ):void
		{
			getInstance().send( "fatal", sender, input );
		}
		
	//--------------------------------------------------------------------------------
	// Protected Static Functions
	//-------------------------------------------------------------------------------- 
        	
		 /**
		 * Creates a new instance of Dragger if one does not currently exist.
		 * 
		 * @return Dragger
		 */
		protected static function getInstance():OpenLogger
		{	
			if ( _singleton == null ) _singleton = new OpenLogger( arguments[ "callee" ] );
            
			return _singleton;      
		}
		
	//--------------------------------------------------------------------------------
	// Protected Functions
	//-------------------------------------------------------------------------------- 
	
    	/**
    	 * 
    	 */	
    	protected function send( logType:String, sender:String, input:* ):void
    	{
    		_localConnection.send( OpenLogger.CONNECTION_NAME, logType, sender, input );
    	}
		
	//--------------------------------------------------------------------------------
	// Event Handlers
	//-------------------------------------------------------------------------------- 
        	
    	/**
    	 * 
    	 */
		protected function onStatus( event:StatusEvent ):void
		{
			trace( event.level );
			trace( event.code );
		}
	}
}